// pages/classic/classic.js
import { ClassicModel } from '../../models/classic.js'
import { UserInfo } from '../../util/userInfo.js'
const classicModel = new ClassicModel()
const hasUser = new UserInfo()

Component({

  /**
   * 页面的初始数据
   */
  properties: {
    cid: Number,
    type: Number
  },

  data: {
    classic: {},
    userInfo: '',
    latest: true,
    first: false,
    index: 1,
    like_status: 0,
    fav_nums: 0
  },


  attached(options) {
    const cid = this.properties.cid
    hasUser.checkUser().then(res => {
      this.setData({
        userInfo: res
      })
    })
    if(!cid) {
      classicModel.getLatest().then(res => {
        wx.setStorageSync('latest', res.index)
        this.setData({
          classic: res,
          index: res.index,
          like_status: res.like_status,
          fav_nums: res.fav_nums
        })
      })
    } else {
      classicModel.getPre(cid).then(res => {
        this.setData({
          classic: res,
          index: res.index,
          like_status: res.like_status,
          fav_nums: res.fav_nums,
          latest: classicModel.isLatest(res.index),
          first: classicModel.isFirst(res.index)
        }) 
      })
    }

  },
  methods: {
    onNext() {
      if(classicModel.isLatest(this.data.classic.index)) {
        return
      }
      let index = this.data.classic.index + 1
      let cla = this._getLocalClassic(index)
      if(cla) {
        this._checkClassic(index)
        classicModel.getFavor(cla.id, cla.type).then(res => {
          this.setData({
            classic: cla,
            like_status: res.like_status,
            fav_nums: res.count,
            latest: classicModel.isLatest(cla.index),
            first: classicModel.isFirst(cla.index)
          })
        })
      } else {
        classicModel.getPre(index).then(res => {
          this._checkClassic(res.index)
          this._saveLocalClassic(res.index, res)
          this.setData({
            classic: res,
            like_status: res.like_status,
            fav_nums: res.fav_nums,
            latest: classicModel.isLatest(res.index),
            first: classicModel.isFirst(res.index)
          })
        })
      }
    },
  
    onPrevious() {
      if(this.data.classic.index === 1) {
        return
      }
      let index = this.data.classic.index -1
      let cla = this._getLocalClassic(index)
      if(cla) {
        this._checkClassic(index)
        classicModel.getFavor(cla.id, cla.type).then(res => {
          this.setData({
            classic: cla,
            like_status: res.like_status,
            fav_nums: res.count
          })
        })
  
      } else {
        classicModel.getPre(index).then(res => {
          this._checkClassic(res.index)
          this._saveLocalClassic(res.index, res)
          this.setData({
            classic: res,
            like_status: res.like_status,
            fav_nums: res.fav_nums,
            latest: classicModel.isLatest(res.index),
            first: classicModel.isFirst(res.index)
          })
        })
      }
    },
  
    onLike(str) {
      let status = str.detail.behavior
      let obj = {
        id: this.data.classic.id,
        type: this.data.classic.type
      }
      if(status === 'like') {
        classicModel.isLike(obj)
      }else {
        classicModel.cancelLike(obj)
      }
    },
    
    _checkClassic(index) {
      if(index === 1) {
        this.setData({
          first: true,
          latest: false
        })
      } else if(index === this.data.index) {
        this.setData({
          first: false,
          latest: true
        })
      } else {
        this.setData({
          first: false,
          latest: false
        })
      }
    },
    _getLocalClassic(index) {
      let key = 'classic'+ index
      if(wx.getStorageSync(key)) {
        return wx.getStorageSync(key)
      } else {
        return false
      }
    },
    _saveLocalClassic(index, obj) {
      let key = 'classic'+ index
      wx.setStorageSync(key, obj)  
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    onGotUserInfo(e) {
      this.setData({
        userInfo: e.detail.userInfo
      })
      wx.setStorageSync('user', e.detail.userInfo)
    },
    // getToekn() {
    //   wx.login({
    //     success(res) {
    //       if(res.code) {
    //         wx.request({
    //           url: 'https://api.yeshaojun.com/api/v1/token/user',
    //           data: {
    //             code: res.code
    //           },
    //           method: 'GET',
    //           success(res) {
    //             console.log('res', res)
    //           }
    //         })
    //       }
    //     }
    //   })
    // }
  }
})